<?php
namespace App\Models;

use Bican\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract;
use Bican\Roles\Models\Role;
use Bican\Roles\Traits\HasRoleAndPermission;
use Illuminate\Http\Request;

/**
 * 管理员模型类
 */
class Admin extends BaseModel implements HasRoleAndPermissionContract
{
    use HasRoleAndPermission;

    public $table = "admins";    //指定表名
    public $allPer = array();    //此用户权限列表

    //表关系区===========================================================================


    //数据逻辑区===========================================================================

    /**
     * 管理员登录验证
     */
    public static function loginValid(Request $request)
    {
        $admin = Admin::where('username', strtolower($request->username))->where('userpass', md5($request->userpass))->first();
        if (empty($admin)) {
            return null;
        }
        return $admin;
    }

    /**
     * 存储当前登录的IP和时间
     */
    public function refreshIP(Request $request)
    {
        $this->lastip = $request->getClientIp();
        $this->lasttime = time();
        return $this->save();
    }

    /**
     * 编辑或则保存新的管理员
     *
     * return true 存储成功 false 存储失败
     */
    public function createOrSave(Request $request)
    {
        $this->username = strtolower($request->username);
        $this->userpass = md5($request->userpass);
        return $this->save();
    }

    /**
     * 检查是否存在同名的账号
     *
     * return true 存在 false 不存在
     */
    public function existUserName($username)
    {
        if (Admin::where('username', strtolower($username))->count() != 0) {
            return true;
        } else {
            return false;
        }
    }

//    /**
//     * 检查当前管理员是否有某项权限
//     * @params $permissions 数组 或则 单个权限slug
//     * @return 只要包含一个权限，返回真
//     */
//    public function rolesCan($permissions)
//    {
//        if ($this->super == 1) {    //超级管理员拥有所有权限
//            return true;
//        }
//
//        //当前管理员的所有权限
//        if (empty($this->allPer)) {
//            $this->allPer = $this->rolePermissions()->get();
//        }
//
//        //检测权限
//        foreach ($this->allPer as $per) {
//            if (is_array($permissions)) {
//                foreach ($permissions as $item) {
//                    if ($per->slug == $item) {
//                        return true;
//                    }
//                }
//            } else {
//                if ($per->slug == $permissions) {
//                    return true;
//                }
//            }
//        }
//
//        return false;
//    }

    //数据验证区===========================================================================

    /**
     * 管理员登录数据验证规则
     * $flag 0 = rules  1 = msg
     */
    public static function loginRulesWithMsg($flag)
    {
        $rules = array(
            "username" => "required|alpha|between:3,50",
            'userpass' => array('Regex:/^(^(?![^a-zA-Z]+$)(?!\D+$)(?![a-zA-Z0-9]+$).{10,}$)/'),
            'yzm'      => 'required|numeric',
        );

        $msg = array(
            "username.required" => "请填写用户名",
            "username.alpha"    => "用户名只能为纯字母",
            "username.between"  => "用户名只能3-50位之间",
            "userpass.regex"    => "密码必须包含大小写字母、数字与特殊符号"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /**
     * 管理员登录数据验证规则
     * $flag 0 = rules  1 = msg
     */
    public static function saveRulesWithMsg($flag)
    {
        $rules = array(
            "username" => "required|alpha|between:3,50",
            'userpass' => array('Regex:/^(^(?![^a-zA-Z]+$)(?!\D+$)(?![a-zA-Z0-9]+$).{10,}$)/'),
        );

        $msg = array(
            "username.required" => "请填写用户名!",
            "username.alpha"    => "用户名只能为纯字母!",
            "username.between"  => "用户名只能3-50位之间!",
            "userpass.regex"    => "密码必须包含大小写字母、数字与特殊符号!",
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    /**
     * 判断角色是否存在
     * 存在 true   不存在 false
     */
    public static function existRoles(Request $request)
    {
        $role = Role::where('name',$request->name)->first();
        if (empty($role)) {
            return false;
        } else {
            return true;
        }
    }
}